home *** CD-ROM | disk | FTP | other *** search
- unit ComServerImpl;
-
- interface
-
- uses
- Windows, ActiveX, Classes, ComObj, ComServer_TLB, StdVcl;
-
- type
- TErrorTrappingTest = class(TTypedComObject, IErrorTrappingTest)
- protected
- {Declare IErrorTrappingTest methods here}
- procedure Error1; safecall;
- procedure Error2; safecall;
- procedure Error3; safecall;
- procedure Error4; safecall;
- procedure Error5; safecall;
- public
- procedure Initialize; override;
- end;
-
- implementation
-
- uses
- ComServ, SysUtils, Forms;
-
- type
- TErrorLogger = class(TInterfacedObject, IServerExceptionHandler)
- protected
- procedure OnException(
- const ServerClass, ExceptionClass, ErrorMessage: WideString;
- ExceptAddr: Integer; const ErrorIID, ProgID: WideString;
- var Handled: Integer; var Result: HResult);
- end;
-
- procedure TErrorTrappingTest.Error1;
- begin
- StrToInt('Hello world')
- end;
-
- procedure TErrorTrappingTest.Error2;
- begin
- (TObject(Application.MainForm) as TScreen).Destroy
- end;
-
- procedure TErrorTrappingTest.Error3;
- begin
- Application.MainForm.ShowModal
- end;
-
- procedure TErrorTrappingTest.Error4;
- begin
- with Application.MainForm do
- Tag := 5 div Tag
- end;
-
- procedure TErrorTrappingTest.Error5;
- begin
- IntToStr(PInteger(nil)^)
- end;
-
- procedure TErrorTrappingTest.Initialize;
- begin
- inherited;
- ServerExceptionHandler := TErrorLogger.Create
- end;
-
- { TErrorLogger }
-
- procedure TErrorLogger.OnException(const ServerClass, ExceptionClass,
- ErrorMessage: WideString; ExceptAddr: Integer; const ErrorIID,
- ProgID: WideString; var Handled: Integer; var Result: HResult);
- var
- Log: TextFile;
- const
- LogName = 'C:\DelphiLog.Txt';
- begin
- AssignFile(Log, LogName);
- if FileExists(LogName) then
- Append(Log)
- else
- Rewrite(Log);
- try
- WriteLn(Log, Format('Class %s (ProgId: %s) raised an %s exception at $%x: %s',
- [ServerClass, ProgID, ExceptionClass, ExceptAddr, ErrorMessage]))
- finally
- CloseFile(Log)
- end;
- //Could kill off the exception like this, but not in this case
- //Handled := Integer(True)
- end;
-
- initialization
- TTypedComObjectFactory.Create(ComServer, TErrorTrappingTest, Class_ErrorTrappingTest,
- ciMultiInstance, tmApartment);
- end.
-